
package com.hlkj.pay.controller.pay;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.pay.PayChannelDictQueryDto;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.infrastructure.model.pay.PayChannelDictDO;
import com.hlkj.pay.service.pay.IPayChannelDictService;
import com.hlkj.pay.vo.pay.req.PayChannelDictQueryReq;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/09/23 11:33
 */
@Tag(name = "支付渠道字典配置")
@RestController
@RequestMapping("/v1/pay/channel/dict")
@Slf4j
@RequiredArgsConstructor
public class PayChannelDictController extends BaseController {

    private final IPayChannelDictService payChannelDictService;

    @Operation(summary = "字典列表查询")
    @GetMapping("")
    CommonResult<List<PayChannelDictDO>> queryPayChannelList(@Validated PayChannelDictQueryReq payChannelDictQueryReq) {
        PayChannelDictQueryDto channelDictQueryDO = BeanUtil.copy(payChannelDictQueryReq, PayChannelDictQueryDto.class);
        if (payChannelDictQueryReq.getDictLevel() != null) {
            if (payChannelDictQueryReq.getDictLevel() == 1) {
                payChannelDictQueryReq.setParentDictKey(null);
            }
            else if (!StringUtils.hasText(payChannelDictQueryReq.getParentDictKey())) {
                // 不是一级分类必须给父编码
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
            }
        }
        if (PayEnums.DICT_KEY.BANK_CODE.getCode().equals(payChannelDictQueryReq.getDictKey())) {
            PayEnums.COMMON_DICT_KEY commonDictKey = PayEnums.COMMON_DICT_KEY.from(payChannelDictQueryReq.getChannelCode());
            if (commonDictKey != null) {
                if ("firstLevel".equals(commonDictKey.getBankCodeParentKey())) {
                    channelDictQueryDO.setParentDictKey(payChannelDictQueryReq.getFirstLevelDictKey());
                }
                else if ("secondLevel".equals(commonDictKey.getBankCodeParentKey())) {
                    channelDictQueryDO.setParentDictKey(payChannelDictQueryReq.getSecondLevelDictKey());
                }
                else if ("threeLevel".equals(commonDictKey.getBankCodeParentKey())) {
                    channelDictQueryDO.setParentDictKey(payChannelDictQueryReq.getThreeLevelDictKey());
                }
            }
        }
        return CommonResult.success(payChannelDictService.queryList(channelDictQueryDO));
    }

    @Operation(summary = "同步字典数据")
    @PostMapping("/sync")
    CommonResult<Void> sync(@RequestBody PayChannelDictQueryReq payChannelDictQueryReq) {

        PayChannelDictQueryDto channelDictQueryDO = BeanUtil.copy(payChannelDictQueryReq, PayChannelDictQueryDto.class);

        List<PayChannelDictDO> updateList = new ArrayList<>();

        switch (payChannelDictQueryReq.getChannelCode()) {
            // 微信支付  bankcode和 bankCityCode 同lakala
            case "yytpay":

                // 银行编码
                if(payChannelDictQueryReq.getDictKey().equals(PayEnums.DICT_KEY.BANK_CODE.getCode())){

                    String excelPath = "D:\\yyt_bankcode.xlsx";
                    // 读取Excel
                    ExcelReader reader = ExcelUtil.getReader(excelPath);
                    // 获取内容，这里假设第一行是标题行
                    List<List<Object>> readAll = reader.read(0);
                   if (CollUtil.isNotEmpty(readAll)) {
                       for (List<Object> row : readAll) {
                           PayChannelDictDO dictDO = new PayChannelDictDO();
                           dictDO.setId(null);
                           dictDO.setChannelCode("yytpay");
                           dictDO.setDictKey("bankCode");
                           dictDO.setDictValue(row.get(0).toString());
                           dictDO.setDictDesc(row.get(1).toString());
                           dictDO.setDictLevel(1);
                           dictDO.setCreateTime(null);
                           dictDO.setUpdateTime(null);
                           updateList.add(dictDO);
                       }
                       payChannelDictService.addList(updateList);
                   }
                }
                break;

            case "alipay":
                if(payChannelDictQueryReq.getDictKey().equals(PayEnums.DICT_KEY.MCC_CATEGORY.getCode())){

                    String excelPath = "D:\\alipay_mccCategory.xlsx";
                    // 读取Excel
                    ExcelReader reader = ExcelUtil.getReader(excelPath);
                    // 获取内容，这里假设第一行是标题行
                    List<List<Object>> readAll = reader.read(0);
                    if (CollUtil.isNotEmpty(readAll)) {
                        // 一级 需要去重
                        Map<String,String> L1 = new HashMap<>();
                        for (List<Object> row : readAll) {
                            L1.put(row.get(0).toString(),row.get(1).toString());
                        }
                        if(MapUtil.isNotEmpty(L1)){
                            for (Map.Entry<String, String> entry : L1.entrySet()) {
                                PayChannelDictDO dictDO = new PayChannelDictDO();
                                dictDO.setId(null);
                                dictDO.setChannelCode("alipay");
                                dictDO.setDictKey("mccCategory");
                                dictDO.setDictValue(entry.getKey());
                                dictDO.setDictDesc(entry.getValue());
                                dictDO.setDictLevel(1);
                                dictDO.setCreateTime(null);
                                dictDO.setUpdateTime(null);
                                updateList.add(dictDO);
                            }
                        }
                        for (List<Object> row : readAll) {
                            PayChannelDictDO dictDO = new PayChannelDictDO();
                            dictDO.setId(null);
                            dictDO.setChannelCode("alipay");
                            dictDO.setDictKey("mccCategory");
                            dictDO.setParentDictKey(row.get(0).toString());
                            dictDO.setDictValue(row.get(0).toString()+"_"+row.get(2).toString());
                            dictDO.setDictDesc(row.get(3).toString());
                            dictDO.setDictLevel(2);
                            dictDO.setCreateTime(null);
                            dictDO.setUpdateTime(null);
                            updateList.add(dictDO);
                        }
                        payChannelDictService.addList(updateList);
                    }
                }
                break;


        }

                return CommonResult.success();
    }





}
